/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.action;

import com.rw.magallanes.core.Category;
import com.rw.magallanes.core.Item;
import com.rw.magallanes.core.Warehouse;
import com.rw.magallanes.ide.main.MainFrame;
import com.rw.magallanes.ide.main.MainSystem;
import com.rw.magallanes.utilities.MessageType;
import com.rw.magallanes.utilities.Utilities;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

/**
 *
 * @author Gonzalez
 */
public class AddCategoryAction extends AbstractAction {

    private static String pattern = "Categoria_";

    @Override
    public void actionPerformed(ActionEvent e) {
        MainSystem ms = MainSystem.getInstance();
        MainFrame mf = ms.getMainFrame();
        mf.stopEditingItemsTable();

        String name = Utilities.name(pattern);
        String input = (String) JOptionPane.showInputDialog(mf, "Nombre", "Nuevo Item", JOptionPane.INFORMATION_MESSAGE, null, null, name);
        if (input != null) {

            if (!input.isEmpty()) {
                Warehouse wh = mf.getWarehouse();
                if (!wh.existsItemName(input)) {
                    Category category = new Category(input);
                    wh.addCategory(category);
                    mf.updateCategoriesTable();
                    mf.updateItemsFor(category);
                } else {
                    Utilities.showMsg(mf, "El item ya existe", MessageType.WARN);
                }
            } else {
                Utilities.showMsg(mf, "Nombre invalido", MessageType.WARN);
            }
        }
    }

    @Override
    public Object getValue(String key) {
        if (key.compareTo(NAME) == 0) {
            return "Agregar Categoria";
        } else {
            return super.getValue(key);
        }
    }
}
